const app = getApp()
var upload = require('../../../utils/upload');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    tempFilePaths: [],
    tempFileNum:0,
    store:{},
  },
  onShow: function () {
    if(app.globalData.appConfig.store.is_store == 0){
      app.wxAlert('非商家身份',()=>{
        wx.navigateBack({
          delta:1
        })
      })
    }else{
      this.data.latitude = app.globalData.appConfig.store.info.latitude
      this.data.longitude = app.globalData.appConfig.store.info.longitude
      this.setData({
        store:app.globalData.appConfig.store.info,
        fileList:[{url:app.globalData.appConfig.store.info.img}]
      })
    }
  },
  /**
   * 提交绑定
   */
  formSubmit: function (e) {
    var data = e.detail.value;
    if (app.util().isNull(data.name)) {
      app.wxAlert('好店名称必须填写');
    }else if (app.util().isNull(data.address)) {
      app.wxAlert('好店地址必须填写');
    } else if (app.util().isNull(data.telphone)) {
      app.wxAlert('联系电话必须填写');
    }else {
      this.setData({
        disabled:true
      })
      this.postFile((file) => {
        if (file.isend == true){
          var parms = {
            name: data.name,
            address: data.address,
            telphone: data.telphone,
            latitude:  this.data.latitude,
            longitude: this.data.longitude,
            imgs: JSON.stringify(file.filePaths)
          }
          app.api().Post('api/v1/smartbc/store/edit', parms,(res)=>{
            this.setData({
              disabled:false
            })
            app.wxAlert(res.msg,()=>{
              wx.navigateBack({
                delta:1
              })
            });
          })
          setTimeout(()=>{
            this.setData({
              disabled:false
            })
          },1500)
        }
      })
    }
  },
  //图片上传
  postFile: function (callback) {
    upload.Upfiles('api/v1/smartbc/config/upimg', this.data.tempFilePaths,callback);
  },
  //上传选择图片
  afterRead:function(event){
    const { file } = event.detail;
    var fileList = this.data.fileList;
    var tempFilePaths = this.data.tempFilePaths;
    fileList.push({url: file.path});
    tempFilePaths.push(file.path);
    this.setData({
      fileList:fileList,
    });
    this.data.tempFilePaths = tempFilePaths;
  },
  //删除图片
  deleteItem: function (res) {
    var tempFilePaths = this.data.tempFilePaths;
    var fileList = this.data.fileList;
    var index = res.target.id;
    if (index > -1) {
      fileList.splice(index,1);
      tempFilePaths.splice(index, 1);
    }
    this.setData({
      fileList:fileList
    })
    this.data.tempFilePaths = tempFilePaths;
  },
  getMapsAddress: function () {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        that.data.latitude = res.latitude
        that.data.longitude = res.longitude
        store.address = res.address
        that.setData({
          store:store
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          //授权成功之后，再调用chooseLocation选择地方
                          wx.chooseLocation({
                            success: function (res) {
                              that.data.latitude = res.latitude
                              that.data.longitude = res.longitude
                              store.address = res.address
                              that.setData({
                                store:store
                              })
                            },
                          })
                        } else {
                          wx.showToast({title: '授权失败',icon: 'success',duration: 1000})
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '调用授权窗口失败',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
    })
  },
  //会员制度
  onRules: function (e) {
    var config = wx.getStorageSync('config');
    wx.navigateTo({
      url: '/pages/helper/webview?src='+app.apiroot+'/app/'+config.app_id+'/smartbc/article/sla'
    })
  },
})